package com.email.box.codeGenerator;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 代码生成器（DTO & VO）
 *
 * @author liza
 */
public class EntityGenerator {

    /**
     * 表名 & 接口API名
     */
    private static final String TABLE_NAME = "sys_er_table_rel";
    private static final String API_NAME = "saveErTableRelSub";

    /**
     * 是否生成 DTO & VO
     */
    private static final boolean CREATE_ENTITY_DTO = true;
    private static final boolean CREATE_ENTITY_VO = false;

    /**
     * 是否覆盖文件
     */
    private static final boolean OVERRIDE_FILE = true;

    public static void main(String[] args) {
        String[] tableNames = {TABLE_NAME};
        String tableName = lineToHump(TABLE_NAME);
        String poU = toUpperCase(tableName) + "PO";
        String poL = toLowerCase(tableName) + "Po";
        String dtoU = toUpperCase(API_NAME) + "DTO";
        String dtoL = toLowerCase(API_NAME) + "Dto";
        String voU = toUpperCase(API_NAME) + "VO";
        String voL = toLowerCase(API_NAME) + "Vo";
        // 控制台执行
        Scanner scan = new Scanner(System.in);
        System.out.println("驱动：\t" + CodeGenerator.DRIVER_NAME);
        System.out.println("Url：\t" + CodeGenerator.URL);
        System.out.println("用户名：\t" + CodeGenerator.USERNAME);
        System.out.println("密码： \t" + CodeGenerator.PASSWORD);
        System.out.println("表：\t" + Arrays.toString(tableNames));
        if (CREATE_ENTITY_DTO) {
            System.out.println("************生成实体DTO************");
        }
        if (CREATE_ENTITY_VO) {
            System.out.println("************生成实体VO************");
        }
        System.out.println("是否继续？");
        String str = scan.nextLine();
        if ("".equals(str)) {
            if (CREATE_ENTITY_DTO) {
                CodeGenerator.codeGenerator(tableNames, true, OVERRIDE_FILE,
                        "entity.dto", dtoU, "templates/entity.java");
            }
            if (CREATE_ENTITY_VO) {
                CodeGenerator.codeGenerator(tableNames, true, OVERRIDE_FILE,
                        "entity.vo", voU, "templates/entity.java");
            }
        }
        System.out.println("==================================================");
        System.out.println("推荐写法：");
        if(CREATE_ENTITY_DTO){
            System.out.println(poU + " " + poL + "(" + dtoU + " dto);");
        }
        if(CREATE_ENTITY_VO){
            System.out.println(voU + " " + voL + "(" + poU + " po);");
            System.out.println("List<" + voU + ">" + " " + voL + "s" + "(" + "List<" + poU + ">" + " pos);");
        }
        System.out.println();
        scan.close();
    }

    /**
     * 下划线转驼峰
     */
    public static String lineToHump(String name) {
        StringBuilder sb = new StringBuilder();
        String[] strings = name.split("_");
        sb.append(strings[0]);
        for (int i = 1; i < strings.length; i++) {
            sb.append(toUpperCase(strings[i]));
        }
        return sb.toString();
    }

    public static String toUpperCase(String name) {
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    public static String toLowerCase(String name) {
        return name.substring(0, 1).toLowerCase() + name.substring(1);
    }
}
